package com.tianruan.dc.modules.sys.user.utils;

import com.tianruan.dc.common.auth.MAuthenticationUser;
import com.tianruan.dc.common.utils.ServletsUtils;
import com.tianruan.dc.common.utils.SpringContextHolder;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.mapper.RoleMapper;
import com.tianruan.dc.modules.sys.user.mapper.UserMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;


public class UserUtils implements Serializable {

    private static UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
    private static RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);

    public static UserDTO getUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        MAuthenticationUser user = null;
        Authentication authentication = context.getAuthentication();
        if (authentication != null)
            user = (MAuthenticationUser) authentication.getPrincipal();
        UserDTO userDTO = new UserDTO();
        try {
            BeanUtils.copyProperties(userDTO, user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return userDTO;
    }

    // 获取用户权限
    public static Collection<? extends GrantedAuthority> getUserAuthorities() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        if (authentication != null) {
            return authentication.getAuthorities();
        }
        return null;
    }

    // 获取用户权限
    public static boolean hasAuthority(String authority) {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();

        if (authentication != null) {
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority grantedAuthority: authorities) {
                if (grantedAuthority.getAuthority().equals(authority)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 是否认证二次密码
     * @return
     */
    public static boolean isAuthSecondPassword() {
        HttpServletRequest request = ServletsUtils.getRequest();
        HttpSession session = request.getSession();
        String esp = (String) session.getAttribute("secondPassword");
        if (esp != null) {
            return true;
        } else {
            return false;
        }
    }

//    /**
//     * 根据登录名获取用户
//     * @param loginName
//     * @return 取不到返回null
//     */
//    public static User getByLoginName(String loginName){
//
//        user = userMapper.getByLoginName(new User(null, loginName));
//        if (user == null){
//            return null;
//        }
//        user.setRoleList(roleDao.findList(new Role(user)));
//
//        return user;
//    }

}
